<template>
  <el-dialog width="60%" :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="12">
          <!-- 商品编码 -->
          <el-form-item label="商品编码"
                        prop="invCode">
            <!--文本框-->
            <el-input v-model="dataForm.invCode"
                      placeholder="商品编码" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 商品名称 -->
          <el-form-item label="商品名称"
                        prop="invName">
            <!--文本框-->
            <el-input v-model="dataForm.invName"
                      placeholder="商品名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 条形码 -->
          <el-form-item label="条形码"
                        prop="barcode">
            <!--文本框-->
            <el-input v-model="dataForm.barcode"
                      placeholder="条形码" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 规格型号 -->
          <el-form-item label="规格型号"
                        prop="invstd">
            <!--文本框-->
            <el-input v-model="dataForm.invstd"
                      placeholder="规格型号" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 品牌 -->
          <el-form-item label="品牌"
                        prop="brandName">
            <!--文本框-->
            <el-input v-model="dataForm.brandName"
                      placeholder="品牌" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 商品分类编码 -->
          <el-form-item label="商品分类编码"
                        prop="sortCode">
            <!--文本框-->
            <el-input v-model="dataForm.sortCode"
                      placeholder="商品分类编码" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 包装单位 -->
          <el-form-item label="包装单位"
                        prop="packageUnit">
            <!--文本框-->
            <el-input v-model="dataForm.packageUnit"
                      placeholder="包装单位" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 商品数量 -->
          <el-form-item label="商品数量"
                        prop="packageQty">
            <!--文本框-->
            <el-input v-model="dataForm.packageQty"
                      placeholder="商品数量" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 最小订货量 -->
          <el-form-item label="最小订货量"
                        prop="minOrderQty">
            <!--文本框-->
            <el-input v-model="dataForm.minOrderQty"
                      placeholder="最小订货量" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 标准销售价 -->
          <el-form-item label="标准销售价"
                        prop="retailPrice">
            <!--文本框-->
            <el-input v-model="dataForm.retailPrice"
                      placeholder="标准销售价" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 采购价 -->
          <el-form-item label="采购价"
                        prop="qualityPeriod">
            <!--文本框-->
            <el-input v-model="dataForm.qualityPeriod"
                      placeholder="采购价" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 是否拆零 -->
          <el-form-item label="是否拆零"
                        prop="isApart">
            <!--文本框-->
            <el-input v-model="dataForm.isApart"
                      placeholder="是否拆零" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 供应商编码 -->
          <el-form-item label="供应商编码"
                        prop="vendorCode">
            <!--文本框-->
            <el-input v-model="dataForm.vendorCode"
                      placeholder="供应商编码" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 备注 -->
          <el-form-item label="备注"
                        prop="remarks">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.remarks"
                      placeholder="备注"></el-input>
          </el-form-item>
        </el-col>

      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        invCode: '',
        invName: '',
        barcode: '',
        invstd: '',
        brandName: '',
        sortCode: '',
        packageUnit: '',
        packageQty: '',
        minOrderQty: '',
        retailPrice: '',
        qualityPeriod: '',
        isApart: '',
        vendorCode: '',
        remarks: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {}
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/mdm/mdmInventoryZp1/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/mdm/mdmInventoryZp1/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
